﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;


/// <summary>
/// 配置实例类
/// </summary>
[System.Serializable]
public class ConfigInfo
{
    public int 屏幕宽 = 1080;
    public int 屏幕高 = 1920;
    public bool 显示光标 = false;
    public float 延迟跳转_秒 = 10f;
    public float 拍照倒计时_秒 = 5f;
    public float 等待时间_秒 = 30f;
    public string 服务器地址 = "http://43.142.146.161/Images/";
    public int 上下移动 = -169;
    public float 画面缩放 = 1f;
    public float 画面旋转 = 0;
    public string KeyId;
    public string KeySecret;
}


public class ConfigMgr : MonoBehaviour
{
    public static ConfigMgr Instance;

    [HideInInspector] public ConfigInfo configInfo = null;

    private string path;

    void Awake()
    {
        Instance = this;

        path = Application.streamingAssetsPath + "/configInfo.xml";

        //加载配置文件
        LoadConfigInfo(path);

        //保存配置
        //SaveConfigInfo();
    }

    private void LoadConfigInfo(string path)
    {
        if (File.Exists(path))
        {
            FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            string text = sr.ReadToEnd();
            configInfo = XmlHelper.DeSerialize<ConfigInfo>(text);
            fs.Close();
        }
    }

    /// <summary>
    /// 保存配置信息到本地
    /// </summary>
    [ContextMenu("生成配置文件")]
    public void SaveConfigInfo()
    {
        //构造当前的信息
        configInfo = new ConfigInfo();

        string xml = XmlHelper.Serialize<ConfigInfo>(configInfo);

        path = Application.dataPath + "/StreamingAssets/configInfo.xml";
        if (File.Exists(path))
        {
            File.Delete(path);
        }

        FileInfo fileInfo = new FileInfo(path);
        if (!fileInfo.Directory.Exists)
        {
            fileInfo.Directory.Create();
        }

        StreamWriter sw = new StreamWriter(path);
        sw.Write(xml);
        sw.Flush();
        sw.Close();

#if UNITY_EDITOR
        UnityEditor.AssetDatabase.Refresh();
#endif
    }
}